package cn.com.smartdevices.bracelet.chart;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.profile.scale.HeartRateInfo;

import cn.com.smartdevices.bracelet.chart.base.BaseChartView;
import cn.com.smartdevices.bracelet.chart.base.PieChart;
import cn.com.smartdevices.bracelet.chart.util.ChartData;

public class DynamicPieChartView extends BaseChartView<PieChart> {
    private static final String f3876a = "Chart.DynamicPieChartView";
    private float f3877b;
    private float f3878c;
    private Animator f3879d;
    private int f3880q;

    public DynamicPieChartView(Context context) {
        this(context, null);
    }

    public DynamicPieChartView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public DynamicPieChartView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.f3855m = new DecelerateInterpolator();
        this.f3856n = 1;
    }

    class C0777s implements ValueAnimator.AnimatorUpdateListener {
        final /* synthetic */ DynamicPieChartView f4029a;

        C0777s(DynamicPieChartView dynamicPieChartView) {
            this.f4029a = dynamicPieChartView;
        }

        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            ((PieChart) this.f4029a.f3847e).rotate(((Float) valueAnimator.getAnimatedValue()).floatValue());
            this.f4029a.postInvalidateOnAnimation();
        }
    }

    class C0774p extends AnimatorListenerAdapter {
        final /* synthetic */ DynamicPieChartView f4026a;

        C0774p(DynamicPieChartView dynamicPieChartView) {
            this.f4026a = dynamicPieChartView;
        }

        public void onAnimationEnd(Animator animator) {
            if (DynamicPieChart.class.isInstance(this.f4026a.f3847e)) {
                ((PieChart) this.f4026a.f3847e).m8672d(-1);
            }
            this.f4026a.m8447c(0.0f);
        }

        public void onAnimationStart(Animator animator) {
            if (DynamicPieChart.class.isInstance(this.f4026a.f3847e)) {
                ((PieChart) this.f4026a.f3847e).m8672d(HeartRateInfo.HR_EMPTY_VALUE);
            }
            this.f4026a.postInvalidateOnAnimation();
        }
    }

    class C0775q implements ValueAnimator.AnimatorUpdateListener {
        final /* synthetic */ DynamicPieChartView f4027a;

        C0775q(DynamicPieChartView dynamicPieChartView) {
            this.f4027a = dynamicPieChartView;
        }

        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            ((PieChart) this.f4027a.f3847e).m8672d(((Integer) valueAnimator.getAnimatedValue()).intValue());
            this.f4027a.postInvalidateOnAnimation();
        }
    }

    class C0776r implements Animator.AnimatorListener {
        final /* synthetic */ DynamicPieChartView f4028a;

        C0776r(DynamicPieChartView dynamicPieChartView) {
            this.f4028a = dynamicPieChartView;
        }

        public void onAnimationCancel(Animator animator) {
        }

        public void onAnimationEnd(Animator animator) {
            if (DynamicPieChart.class.isInstance(this.f4028a.f3847e)) {
                ((DynamicPieChart) this.f4028a.f3847e).m8707b();
                ((PieChart) this.f4028a.f3847e).rotate(0.0f);
            }
            this.f4028a.postInvalidateOnAnimation();
        }

        public void onAnimationRepeat(Animator animator) {
        }

        public void onAnimationStart(Animator animator) {
            if (DynamicPieChart.class.isInstance(this.f4028a.f3847e)) {
                ((DynamicPieChart) this.f4028a.f3847e).m8703a();
                ((PieChart) this.f4028a.f3847e).rotate(0.0f);
            }
            this.f4028a.postInvalidateOnAnimation();
        }
    }



    private void m8433e() {
        float abs = Math.abs(this.f3878c - this.f3877b) / ((PieChart) this.f3847e).getMaxValue();
        if (abs > 1.0f) {
            abs = 1.0f;
        }
        this.f3856n = (long) (Math.sqrt((double) abs) * 800.0d);
        if (this.f3856n == 0) {
            this.f3856n = 1;
        }
        this.f3858p = animRefresh(this.f3856n);
    }

    @SuppressLint({"NewApi"})
    private Animator m8434f() {
        ValueAnimator ofFloat = ValueAnimator.ofFloat(new float[]{0.0f, 1.0f});
        ofFloat.addListener(new C0776r(this));
        ofFloat.addUpdateListener(new C0777s(this));
        ofFloat.setDuration(3500);
        ofFloat.setInterpolator(new LinearInterpolator());
        ofFloat.setRepeatMode(1);
        ofFloat.setRepeatCount(-1);
        return ofFloat;
    }

    public void m8442a(float f) {
        ((PieChart) this.f3847e).setMaxValue(f);
    }

    public void m8443a(int i) {
        this.f3880q = i;
        if (i != PersonInfo.INCOMING_CALL_DISABLE_BIT) {
            this.f3847e = new DynamicPieChart(getContext());
        } else {
            this.f3847e = new XMDynamicWeightViewUnit(getContext());
        }
        if (DynamicPieChart.class.isInstance(this.f3847e)) {
            ((DynamicPieChart) this.f3847e).m8704a(i);
        }
        invalidate();
    }

    public void m8444a(long j) {
        if (this.f3879d == null) {
            this.f3879d = m8434f();
        }
        if (!this.f3879d.isStarted()) {
            if (j > 0) {
                this.f3879d.setStartDelay(j);
            }
            this.f3879d.start();
        }
    }

    public void m8445b() {
        m8444a(0);
    }

    public void m8446b(float f) {
        this.f3877b = f;
        m8433e();
    }

    public void m8447c(float f) {
        if (this.f3880q == PersonInfo.INCOMING_CALL_DISABLE_BIT) {
            this.f3877b = this.f3878c;
        }
        if (f > ((PieChart) this.f3847e).getMaxValue()) {
            this.f3878c = ((PieChart) this.f3847e).getMaxValue();
        } else {
            this.f3878c = f;
        }
        if (this.f3880q == ChartData.f4049d) {
            postInvalidate();
        } else {
            m8433e();
        }
    }

    public void c_() {
        ValueAnimator ofInt = ValueAnimator.ofInt(new int[]{HeartRateInfo.HR_EMPTY_VALUE, 0});
        ofInt.addListener(new C0774p(this));
        ofInt.addUpdateListener(new C0775q(this));
        ofInt.setDuration(1000);
        ofInt.setInterpolator(new LinearInterpolator());
        ofInt.start();
    }

    public void m8448d() {
        if (this.f3879d != null) {
            this.f3879d.end();
        }
    }

    public void m8449d(float f) {
        if (XMDynamicWeightViewUnit.class.isInstance(this.f3847e)) {
            ((XMDynamicWeightViewUnit) this.f3847e).m8843b(f);
        }
    }

    public void setScaleIcon(int f) {
        if (XMDynamicWeightViewUnit.class.isInstance(this.f3847e)) {
            ((XMDynamicWeightViewUnit) this.f3847e).setScaleIcon(f);
        }
    }

    protected void onDraw(Canvas canvas) {
        if (this.f3857o < 1.0f) {
            ((PieChart) this.f3847e).setValue(((this.f3878c - this.f3877b) * this.f3857o) + this.f3877b);
            ((PieChart) this.f3847e).draw(canvas, 1.0f);
            return;
        }
        ((PieChart) this.f3847e).setValue(this.f3878c);
        ((PieChart) this.f3847e).draw(canvas, this.f3857o);
    }
}
